<?php

/**
 * 用户相关接口
 * File Name：UserController.php
 * File Encoding：UTF-8
 * File New Time：2014-10-21 16:18:43
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class UserController extends SSOServer {

    //登录验证
    public function actionLogin() {
        if (empty($this->data['username'])) {
            $this->error('用户名不能为空！');
        }
        if (empty($this->data['password'])) {
            $this->error('用户密码不能为空！');
        }
        $model = MembersModel::model();
        $type = (int) $this->data['type']; //0用户名，1用户ID，2用户邮箱
        switch ($type) {
            case 0:
                $data = $model->find($model->where(array('username' => $this->data['username'], 'password' => $model->hashPassword($this->data['password']))));
                break;
            case 1:
                $data = $model->find($model->where(array('uid' => (int) $this->data['username'], 'password' => $model->hashPassword($this->data['password']))));
                break;
            case 2:
                $data = $model->find($model->where(array('email' => $this->data['username'], 'password' => $model->hashPassword($this->data['password']))));
                break;
            default:
                $data = $model->find($model->where(array('username' => $this->data['username'], 'password' => $model->hashPassword($this->data['password']))));
                break;
        }
        if (empty($data)) {
            $this->error('用户名或者密码不正确！');
        }
        $this->returnData($data->attributes);
    }

    //获取用户信息
    public function actionGetuser() {
        $model = MembersModel::model();
        if (empty($this->data['username'])) {
            $this->error('用户名不能为空！');
        }
        if ($this->data['isuid']) {
            $data = $model->findByPk($this->data['username']);
        } else {
            $data = $model->find($model->where(array('username' => $this->data['username'])));
        }
        if (empty($data)) {
            $this->error('该用户不存在！');
        }
        $this->returnData($data->attributes);
    }

    //注册用户
    public function actionRegister() {
        $uid = UserRegister::getInstance()->data($this->data)->register();
        if (empty($uid)) {
            $this->error(UserRegister::getInstance()->getError());
        }
        if ($this->data['members_taobao']['wangwang']) {
            MembersTaobaoModel::model()->saveWangwang($uid, $this->data['members_taobao']['wangwang']);
        }
        $this->returnData($uid);
    }

    //编辑用户基本信息
    public function actionEdit() {
        if (empty($this->data['username'])) {
            $this->error('用户名不能为空！');
        }
        $model = MembersModel::model();
        $data = $model->find($model->where(array('username' => $this->data['username'])));
        if (empty($data)) {
            $this->error('该用户不存在，无法更新！');
        }
        //是否验证密码
//        if ($this->data['ignoreoldpw'] && $data['password'] != $model->hashPassword($this->data['oldpw'])) {
//            $this->error('旧密码错误！');
//        }
        unset($this->data['password']);
        $newData = $this->data;
        //更新密码
        if ($this->data['newpw']) {
            $newData['password'] = $model->hashPassword($this->data['newpw']);
            unset($newData['newpw']);
        }
        //淘宝旺旺内容
        $members_taobao = $newData['members_taobao'];
        if (empty($newData)) {
            $this->returnData();
        }
        $data->attributes = $newData;
        //更新
        if ($data->validate() && $data->save()) {
            //tk_members_taobao
            if (!empty($members_taobao) && is_array($members_taobao)) {
                $MembersTaobaoModel = MembersTaobaoModel::model();
                $taobao = $MembersTaobaoModel->find($model->where(array('uid' => $data->uid)));
                if (empty($taobao)) {
                    $MembersTaobaoModel->attributes = $members_taobao;
                    $MembersTaobaoModel->setIsNewRecord(true);
                    $MembersTaobaoModel->save();
                } else {
                    $taobao->attributes = $members_taobao;
                    $taobao->save();
                }
            }
            $this->synEdit($data->uid, $newData);
            $this->returnData();
        } else {
            $error = $data->getError();
            $this->error($error ? $error : '资料更新失败！');
        }
    }

    //同步编辑
    protected function synEdit($uid, $post) {
        if (empty($uid)) {
            return false;
        }
        $url = array();
        $time = time();
        foreach ($this->apps as $appid => $app) {
            if ($app['synlogin'] && $appid != $this->client_id) {
                $url[$appid] = $app['url'] . '?time=' . $time . '&code=' . urlencode(Encrypt::authcode("action=updatauser&uid={$uid}&time=" . $time, 'ENCODE', $app['authkey']));
                if (YII_DEBUG == false) {
                    $data = array(
                        'url' => $url[$appid],
                        'post' => serialize($post),
                    );
                    RedisCluster::getInstance()->push('sso_synchronous', $data);
                }
            }
        }
        return true;
    }

    //删除用户
    public function actionDelete() {
        if (empty($this->data['username'])) {
            $this->error('用户名不能为空！');
        }
        $model = MembersModel::model();
        if ($this->data['isuid']) {
            $info = $model->findByPk($this->data['username']);
            if ($info && $info->delete()) {
                $this->synDelete($info->uid);
                $this->returnData();
            }
        } else {
            $info = $model->find($model->where(array('username' => $this->data['username'])));
            if ($info && $info->delete()) {
                $this->synDelete($info->uid);
                $this->returnData();
            }
        }
        $error = $model->getError();
        $this->error($error ? $error : '用户删除失败！');
    }

    //同步删除
    protected function synDelete($uid) {
        if (empty($uid)) {
            return false;
        }
        $url = array();
        $time = time();
        foreach ($this->apps as $appid => $app) {
            if ($app['synlogin'] && $appid != $this->client_id) {
                $url[$appid] = $app['url'] . '?time=' . $time . '&code=' . urlencode(Encrypt::authcode("action=deleteuser&uid={$uid}&time=" . $time, 'ENCODE', $app['authkey']));
                if (YII_DEBUG == false) {
                    $data = array(
                        'url' => $url[$appid],
                        'post' => '',
                    );
                    RedisCluster::getInstance()->push('sso_synchronous', $data);
                }
            }
        }
        return true;
    }

}
